﻿/////////////////////////////////////////////////////////////////
//File: CommentInfo.cs
//Description: 
//  Ajax 代码
//  
//Copyright© 2011 Moliu
//History:
//  2011-3-30 21:59:07    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;

namespace My.Blog.Web.UI_MAF_Template
{
    public class CommentList : My.Blog.Web.Controllers.BasePage
    {
        public CommentList() 
        {
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_EMPTY = "If:Empty";
            const string IF_NOT_EMPTY = "IfNot:Empty";
            const string IF_MAINTAIN = "If:Maintain";
            const string LOOP_LIST = "Loop:List";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/CommentList.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 模板处理对象
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplEntity;
            Template tplLoopEntity;

            NameValueCollection mapLogic = new NameValueCollection();
            NameValueCollection mapEntity = new NameValueCollection();
            IList<NameValueCollection> mapLoopEntity;
            
            // 数据源
            CommentHandler _CommentHandler = new CommentHandler(this);
            IList<CommentInfo> list = _CommentHandler.ReadCommentsByConditions();

            if (0 == list.Count)
            {
                //mapLogic.Add(IF_NOT_EMPTY, "");
                tplLogic.Replace(IF_NOT_EMPTY, "");
                context.Response.Write(tplLogic.Result);
            }
            else
            {
                mapEntity.Add("{RootUrl}", this.RootUrl);
                mapEntity.Add("{ArticleId}", this.ArticleId.ToString());
                        
                mapLogic.Add(IF_EMPTY, "");
                if (true != this.IsMasterMode)
                    mapLogic.Add(IF_MAINTAIN, "");
                
                // 替换循环子模板
                mapLoopEntity = MAF.Common.ObjectConverter.ToNameValueCollectionList<CommentInfo>(list, @"{Key}", "");
                tplLoopEntity = new Template(tplLogic.Find(LOOP_LIST).Groups[2].Value);
                tplLoopEntity.Replace(mapLoopEntity);
                
                // 替换逻辑模板
                mapLogic.Add(LOOP_LIST, tplLoopEntity.Result);
                tplLogic.Replace(mapLogic);

                // 替换实体模板
                tplEntity = new Template(tplLogic.Result);
                tplEntity.Replace(mapEntity);

                context.Response.Write(tplEntity.Result);
            }
        }
    }
}